package com.zz.fileproviderlibrary.fileprovider;

import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;

/**
 * @author: zhuozhang6
 * @date: 2021/12/13
 * @email: zhuozhang6@iflytek.com
 * @Description:
 */
public class UriUtils {

    private static final String TAG = UriUtils.class.getSimpleName();

    public static Uri getUri(Context context, File file) {
        if (context == null || file == null)
            return null;

        Uri uri = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = MyFileProvider.getUriForFile(context, context.getPackageName() + ".shareFileProvider", file);
        } else {
            uri = Uri.fromFile(file);
        }
        return uri;
    }

    public static Uri getUri(Context context, String filePath) {
        if (context == null || TextUtils.isEmpty(filePath))
            return null;
        try {
            File file = new File(filePath);
            return getUri(context, file);
        } catch (Exception e) {
            Log.e(TAG, "error file:" + e.getLocalizedMessage());
        }
        return null;
    }
}
